// 技能详情页面逻辑
const apiService = require('../../../js/services/api/sharedDatabaseService');

Page({
  data: {
    skill: null,
    loading: true
  },

  onLoad: function (options) {
    wx.setNavigationBarTitle({ title: '技能详情' });
    
    this.setData({ loading: true, skill: null });
    
    const skillId = options.id || '1';
    this.loadSkillDetail(skillId);
  },

  /**
   * 加载技能详情
   */
  async loadSkillDetail(skillId) {
    if (!skillId || skillId === 'undefined') {
      skillId = '1';
    }
    
    // 使用安全的请求包装器
    const result = await apiService.safeRequest(
      () => apiService.getSkillDetail(skillId),
      this.getMockSkillDetail(skillId)
    );
    
    this.setData({
      skill: result?.data || result,
      loading: false
    });
  },

  /**
   * 获取模拟技能详情数据
   */
  getMockSkillDetail(skillId) {
    const mockSkills = {
      '1': {
        id: 1,
        name: '电脑维修',
        category: '维修服务',
        description: '提供电脑软硬件维修服务，经验丰富。擅长解决各种电脑故障，包括系统重装、硬件升级、病毒清理等。',
        serviceMethod: 'offline',
        contactInfo: '13500135004',
        serviceTime: '工作日晚上和周末',
        serviceArea: '小区内及周边',
        price: '50-200元',
        experience: '5年维修经验',
        status: 'published',
        createTime: '2025-01-27',
        tags: ['电脑维修', '系统重装', '硬件升级']
      },
      '2': {
        id: 2,
        name: '英语辅导',
        category: '教育培训',
        description: '英语专业毕业，可提供小学到高中英语辅导。教学方法灵活，注重口语训练和语法基础。',
        serviceMethod: 'both',
        contactInfo: '13700137005',
        serviceTime: '周末全天',
        serviceArea: '线上或小区内',
        price: '80-150元/小时',
        experience: '3年教学经验',
        status: 'published',
        createTime: '2025-01-27',
        tags: ['英语辅导', '口语训练', '语法教学']
      },
      '3': {
        id: 3,
        name: '手工编织',
        category: '生活服务',
        description: '擅长毛衣、围巾等编织，可教授编织技巧。作品精美，适合初学者学习。',
        serviceMethod: 'offline',
        contactInfo: '13400134006',
        serviceTime: '每周三下午',
        serviceArea: '小区内',
        price: '30-100元',
        experience: '10年编织经验',
        status: 'published',
        createTime: '2025-01-27',
        tags: ['手工编织', '毛衣编织', '围巾编织']
      },
      '4': {
        id: 4,
        name: '心理咨询',
        category: '咨询服务',
        description: '专业心理咨询师，提供心理健康咨询服务。擅长情绪管理、压力缓解、人际关系等咨询。',
        serviceMethod: 'online',
        contactInfo: '13300133007',
        serviceTime: '工作日晚上',
        serviceArea: '线上咨询',
        price: '200-300元/次',
        experience: '8年咨询经验',
        status: 'published',
        createTime: '2025-01-27',
        tags: ['心理咨询', '情绪管理', '压力缓解']
      },
      '5': {
        id: 5,
        name: '水管维修',
        category: '维修服务',
        description: '可修理常见水管漏水问题，更换水龙头、水管等配件。经验丰富，快速响应。',
        serviceMethod: 'offline',
        contactInfo: '13900139008',
        serviceTime: '全天服务',
        serviceArea: '小区内及周边3公里',
        price: '80-300元',
        experience: '15年水电维修经验',
        status: 'published',
        createTime: '2025-01-28',
        tags: ['水管维修', '漏水修复', '水龙头更换']
      },
      '6': {
        id: 6,
        name: '小学英语课程辅导',
        category: '教育培训',
        description: '专业英语教师，专注小学英语教学。采用趣味教学方法，提高学生学习兴趣。',
        serviceMethod: 'both',
        contactInfo: '13600136009',
        serviceTime: '工作日晚上和周末',
        serviceArea: '线上或小区内',
        price: '60-120元/小时',
        experience: '6年教学经验',
        status: 'published',
        createTime: '2025-01-28',
        tags: ['小学英语', '趣味教学', '基础训练']
      }
    };
    
    return mockSkills[skillId] || mockSkills['1'];
  },

  /**
   * 获取服务方式文本
   */
  getServiceMethodText: function(method) {
    console.log('进入getServiceMethodText函数');
    console.log('serviceMethod原始值:', method);
    console.log('method类型:', typeof method);
    console.log('method是否为null:', method === null);
    console.log('method是否为undefined:', method === undefined);
    console.log('method是否为空字符串:', method === '');
    console.log('method.trim()结果:', method ? method.trim() : 'undefined');
    console.log('method.trim()是否为空:', method ? method.trim() === '' : true);

    // 检查是否有值且不为空
    if (method !== null && method !== undefined && method.trim() !== '') {
      console.log('条件满足，返回method值:', method);
      return method;
    } else {
      console.log('条件不满足，返回默认值: 未知');
      return '未知';
    }
  },

  /**
   * 联系技能提供者
   */
  contactProvider() {
    const skill = this.data.skill;
    if (!skill || !skill.contactInfo) {
      wx.showToast({
        title: '暂无联系方式',
        icon: 'none'
      });
      return;
    }

    wx.showActionSheet({
      itemList: ['拨打电话', '复制号码'],
      success: (res) => {
        if (res.tapIndex === 0) {
          wx.makePhoneCall({
            phoneNumber: skill.contactInfo
          });
        } else if (res.tapIndex === 1) {
          wx.setClipboardData({
            data: skill.contactInfo,
            success: () => {
              wx.showToast({
                title: '号码已复制',
                icon: 'success'
              });
            }
          });
        }
      }
    });
  },

  /**
   * 分享技能信息
   */
  shareSkill() {
    const skill = this.data.skill;
    if (!skill) return;

    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    });
  },

  /**
   * 用户分享
   */
  onShareAppMessage() {
    const skill = this.data.skill;
    return {
      title: skill ? `${skill.name} - ${skill.category}` : '技能共享',
      path: `/pages/neighborhood-help/skill-sharing/detail?id=${skill?.id || 1}`,
      imageUrl: '/img/share-skill.jpg'
    };
  }
});